package com.linksfield.topup.utils;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author
 * @ClassName: UploadUtil
 * @Description: 上传文件
 * @date 2019年12月31日
 * @time 上午10:28:48
 */
@Component
public class UploadUtil {

    private static Logger logger = Logger.getLogger(UploadUtil.class);

    /**
     * 图片上传
     *
     * @param file
     * @param partnerCode
     * @return
     */
    public static String uploadPic(MultipartFile file, String partnerCode, String configType, String type) {

        Map<String, String> map = new HashMap<String, String>();

        try {

            //获取文件大小
            long size = file.getSize();

            if (size > 1 * 1048576) {
                return "请上传小于1兆的图片！";
            } else {

                logger.info("开始上传图片：" + file.getOriginalFilename());

                String originalFilename = file.getOriginalFilename();
                String extendName = originalFilename.substring(originalFilename.lastIndexOf("."), originalFilename.length());

                if (extendName.equalsIgnoreCase(".jpg") || extendName.equalsIgnoreCase(".jpeg") || extendName.equalsIgnoreCase(".png") || extendName.equalsIgnoreCase(".gif")
                        || extendName.equalsIgnoreCase(".bmp") || extendName.equalsIgnoreCase(".psd") || extendName.equalsIgnoreCase(".tiff") || extendName.equalsIgnoreCase(".tga")
                        || extendName.equalsIgnoreCase(".eps")) {
                    //网络地址
                    String path = "/home/pic/";
                    //String path = "D:/logs/linksfield/";
                    String fileName = partnerCode + "_" + configType + "_" + type + "_" + System.currentTimeMillis() + extendName;
                    File dir = new File(path, fileName);
                    File filepath = new File(path);

                    if (!filepath.exists()) {
                        filepath.mkdirs();
                    }
                    file.transferTo(dir);

                    logger.info("picPath：https://wx.linksfield.net/pic/" + fileName);

                    return "https://wx.linksfield.net/pic/" + fileName;

                } else {
                    logger.info("文件格式有误，上传失败：" + file);
                    return "请传图片格式的文件！";
                }

            }

        } catch (Exception e) {
            logger.info("文件出现异常，上传失败");
            return "图片上传出现异常！";
        }

    }

    /**
     * 是否是中文
     *
     * @param c
     * @return
     */

    public static boolean isChinese(char c) {

        Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);

        if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS

                || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS

                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A

                || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION

                || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION

                || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {

            return true;

        }

        return false;

    }

    /**
     * 是否是英文
     *
     * @param
     * @return
     */

    public static boolean isEnglish(String charaString) {

        return charaString.matches("^[a-zA-Z]*");

    }

    /**
     * 是否是中文
     *
     * @param str
     * @return
     */
    public static boolean isChinese(String str) {

        String regEx = "[\\u4e00-\\u9fa5]+";

        Pattern p = Pattern.compile(regEx);

        Matcher m = p.matcher(str);

        if (m.find()) {
            return true;
        } else {
            return false;
        }


    }


}
